package cn.wildfire.chat.kit.conversation.message.viewholder;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sourcemuch.im.caiba.R;
import com.sourcemuch.im.caiba.R2;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.wildfire.chat.app.extendsion.classes.UserCard;
import cn.wildfire.chat.app.extendsion.widgets.CircleImageView;
import cn.wildfire.chat.kit.annotation.EnableContextMenu;
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.annotation.ReceiveLayoutRes;
import cn.wildfire.chat.kit.annotation.SendLayoutRes;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.user.UserInfoActivity;
import cn.wildfirechat.message.UserCardMessageContent;
import cn.wildfirechat.remote.ChatManager;

/**
 * Create by HEKE, 2019/9/23 16:03
 * TODO
 */
@MessageContentType(UserCardMessageContent.class)
@SendLayoutRes(resId = R2.layout.conversation_item_usercard_send)
@ReceiveLayoutRes(resId = R2.layout.conversation_item_usercard_receive)
@EnableContextMenu
public class UsercardMessageContentViewHolder extends NormalMessageContentViewHolder implements View.OnClickListener {

    @BindView(R2.id.usercardLayout)
    LinearLayout usercardLayout;
    @BindView(R2.id.usercardPortraitImg)
    CircleImageView usercardPortraitImg;
    @BindView(R2.id.usercardNameTxtView)
    TextView usercardNameTxtView;

    protected UserCard userCard;

    public UsercardMessageContentViewHolder(FragmentActivity activity, RecyclerView.Adapter adapter, View itemView) {
        super(activity, adapter, itemView);
    }

    @Override
    public void onBind(UiMessage message) {
        try {
            usercardLayout.setOnClickListener(this);
            UserCardMessageContent userCardMessageContent = (UserCardMessageContent) message.message.content;
            userCard = (UserCard) new UserCard().toGsonObject(userCardMessageContent.content, UserCard.class);
            usercardNameTxtView.setText("" + userCard.userName);
            Glide.with(getContext()).load(userCard.portraitUrl).apply(new RequestOptions().placeholder(R.mipmap.default_header)).into(usercardPortraitImg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        try {
            Intent intent = new Intent(getContext(), UserInfoActivity.class);
            intent.putExtra("userInfo", ChatManager.Instance().getUserInfo(userCard.userId, false));
            intent.putExtra("commonGroupLayoutShow", false);
            getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

